<template>
	<view class="">
		<view class="inp_warp">
			<view class="inp_title key">仓库<span>*</span></view>
			<view class="form_right" @click="changeWarehouse">
				<view :class="warehouseObj.id ? 'valueActive' : '' ">
					{{warehouseObj.id ? warehouseObj.name : '点击选择'}}</view>
				<view class="iconfont icon-you"></view>
			</view>
		</view>
		<view class="inp_warp">
			<view class="inp_title key">供应商<span>*</span></view>
			<view class="form_right" @click="changeSuplier">
				<view :class="supplierObj.id ? 'valueActive' : '' ">
					{{supplierObj.id ? supplierObj.name : '点击选择'}}</view>
				<view class="iconfont icon-you"></view>
			</view>
		</view>
		<!-- 添加产品 -->
		<view class="inp_warp">
			<view class="inp_title key">入库产品<span>*</span></view>
			<view class="add_in" @click="addPart">
				+选择产品
			</view>
		</view>
		<view class="pro_warp" v-if="productList.length != 0">
			<view class="li_warp" v-for="(item,index) in productList" :key="index">
				<view class="cell_con">
					<view class="cell_con_top">
						<view class="contitle">{{item.name}}</view>
						<image :src="BASE_IMG_URL+'/delete1.png'" @click="delProduct(index)" class="deleimg" mode=""></image>
					</view>
					<view class="pro_con_box">
						<view class="boxleft">
							<!-- <view class="context">规格：{{item.specs?item.specs.name:''}}</view> -->
							<view class="context">单位：{{item.unit}}</view>
							<view class="context">
								零售价：￥<input type="digit" v-model="item.price" class="productPrice" placeholder="价格">
							</view>
							<view class="context">
								批发价：￥<input type="digit" v-model="item.wholesale" class="productPrice" placeholder="价格">
							</view>
						</view>
						<view class="conbot">
							<view class="conright">
								<uni-number-box :min="1" v-model="item.number" @change="(e)=>{changeNumber(e,index)}"></uni-number-box>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<form-item v-if="listForm" :listForm="listForm" @changeData="changeOtherData">
		</form-item>
		<!-- 固定审批 -->
		<fixed-approval v-if="approvalPro.status == 1" :list="approvalPro.stepList"></fixed-approval>
		<!-- 审核信息 -->
		<examine-people v-if="approvalPro.status == 0" :list="remindPeople" @delItem="delItem"></examine-people>
		<view class="button" @click="$noMultipleClicks(toSave)">
			保存
		</view>
		<view class="botsec"></view>
		<!-- 选择仓库 -->
		<select-warehouse ref="warehouseChild" @getWarehouse="getWarehouse"></select-warehouse>
		<!-- 添加产品 -->
		<popup-list ref="productChild" @selectList="selectList"></popup-list>
		<!-- 选择供应商 -->
		<supplier-pop ref="supplierChild" @getSupplier="getSupplier"></supplier-pop>
	</view>
</template>

<script>
	import { netAddWarehouse, netSetForm, netApprovalProcess } from '@/api/api.js'
	import { BASE_IMG_URL } from '@/api/http.js'
	import { commonFun } from '@/common/navigate.js'
	import selectWarehouse from '@/components/selectWarehouse.vue'
	import popupList from '@/components/selectProduct/index'
	import formItem from '@/components/form/formitem.vue'
	import supplierPop from '@/components/selectSupplier.vue'
	
	export default{
		mixins:[commonFun],
		components:{
			selectWarehouse,
			popupList,
			formItem,
			supplierPop,
		},
		data() {
			return{
				BASE_IMG_URL:BASE_IMG_URL,
				noClick: true, //防止  重复点击
				warehouseObj:{},   //仓库
				supplierObj:{},  //供应商
				productList:[],
				approvalPro:{},  //审批流程
				remindPeople: [], //提醒谁看
				remindType:'instock',
				listForm:null
			}
		},
		onLoad() {
			this.getForm()
			//审批流程
			this.getProcess()
		},
		onShow() {
			if(this.approvalPro.status == 0) {
				this.getDefaultRemind()
			}
		},
		methods:{
			//选择供应商
			changeSuplier() {
				this.$refs.supplierChild.init()
			},
			getSupplier(obj) {
				this.supplierObj = obj
			},
			//选择仓库
			changeWarehouse() {
				this.$refs.warehouseChild.init()
			},
			getWarehouse(obj) {
				this.warehouseObj = obj
			},
			//选择产品
			addPart() {
				this.$refs.productChild.init()
			},
			selectList(arr) {
				arr.forEach(ele=>{
					ele.minprice = ele.price
					ele.parts = []
					ele.freight = ''
				})
				this.productList = this.deWeight(arr.concat(this.productList))
			},
			deWeight(arr) {
			    for (var i = 0; i < arr.length - 1; i++) {
			        for (var j = i + 1; j < arr.length; j++) {
			            if (arr[i].id == arr[j].id) {
			                arr.splice(j, 1);
			                j--;
			            }
			        }
			    }
			    return arr;
			},
			//修改产品数量
			changeNumber(e,index){
				let obj = this.productList[index]
				obj.number = e
				this.$set(this.productList,index,obj)
			},
			delProduct(index) {
				this.productList.splice(index,1)
			},
			//审批流程
			getProcess() {
				netApprovalProcess({type:'instock'}).then(res=>{
					this.approvalPro = res.data
					if(this.approvalPro.status == 0) {
						this.getDefaultRemind()
					}
				})
			},
			getForm() {
				netSetForm({type:'instock'}).then(res=>{
					res = res.data.data
					this.listForm = res
				})
			},
			changeOtherData(name, value, obj) {
				let arr = this.listForm
				let item = this.changeFormData(name,value,obj)
				arr.forEach((ele, index) => {
					if (ele.id == obj.id) {
						this.$set(this.listForm, index, item)
					}
				})
			},
			toSave() {
				if(!this.warehouseObj.id){
					uni.showToast({
						title:'请选择仓库',
						icon:'none'
					})
					return
				}
				if(!this.supplierObj.id){
					uni.showToast({
						title:'请选择供应商',
						icon:'none'
					})
					return
				}
				if(this.productList.length == 0){
					uni.showToast({
						title:'请选择产品',
						icon:'none'
					})
					return
				}
				let product = []
				this.productList.forEach(ele=>{
					product.push({
						product_id:ele.id,
						number:ele.number,
						price:ele.price,
						freight:ele.freight,
						wholesale:ele.wholesale
					})
				})
				let params = this.checkCommonForm(this.listForm)
				if(!params){
					return
				}
				if (this.remindPeople.length == 0 && this.approvalPro.status == 0) {
					uni.showToast({
						title: '请选择审核人',
						icon: 'none'
					})
					return
				}
				let arr = []
				this.remindPeople.forEach(ele => {
					arr.push(ele.id)
				})
				params = Object.assign({},params,{
					warehouse_id:this.warehouseObj.id,
					supplier_id:this.supplierObj.id,
					product,
					flow_staff_ids:this.approvalPro.status == 0 ? arr.join(',') : ''
				})
				netAddWarehouse(params).then(res=>{
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},2000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.inp_warp {
		padding:30rpx;
		background: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1rpx solid #EDEDED;
		.inp_title {
			font-size: 26rpx;
		}
	
		.form_right {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			color: #999;
			min-width:450rpx;
			text-align: right;
			image {
				width: 40rpx;
				height: 40rpx;
				margin: 28rpx 5rpx 0 0;
			}
			.rightinput{
				width:100%;
				text-align: right;
			}
		}
	}
	.add_in{
		background-color: $uni-color-primary;
		color:#fff;
		height:60rpx;
		line-height: 60rpx;
		padding:0 15rpx;
		font-size: 24rpx;
		border-radius: 10rpx;
	}
	.pro_warp{
		padding:30rpx 24rpx 0 24rpx;
		background:#fff;
		border-bottom:1rpx solid #f5f5f5;
		.li_warp{
			padding-bottom:24rpx;
			margin-bottom:24rpx;
			border-bottom:2rpx solid #f5f5f5;
			&:last-child{
				border-bottom:0;
			}
			.cell_con{
				padding:0 40rpx;
				.cell_con_top{
					display: flex;
					justify-content: space-between;
					align-items: center;
					.contitle{
						font-size:28rpx;
						color:#333;
					}
					.deleimg{
						width:44rpx;
						height:44rpx;
					}
				}
				.pro_con_box{
					display: flex;
					justify-content: space-between;
					align-items: center;
					.context{
						font-size:26rpx;
						color:#666;
						margin-top:10rpx;
						display: flex;
						justify-content: flex-start;
						align-items: center;
						.productPrice{
							width:150rpx;
							background:#f5f5f5;
							text-align: center;
						}
					}
					.conbot{
						margin-top:10rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;
						.conmoney{
							font-size:26rpx;
							color:#333;
						}
					}
				}
				.config_set{
					margin-top:24rpx;
					.config_title{
						font-size:24rpx;
						color:#333;
						display: flex;
						justify-content: flex-start;
						align-items: center;
						.addConimg{
							width:100rpx;
							height:40rpx;
							margin-left:10rpx;
						}
					}
					.config_list{
						margin-top:24rpx;
						.config_li{
							display: flex;
							justify-content: flex-start;
							align-items: center;
							margin-top:24rpx;
							border-bottom:1rpx solid #f5f5f5;
							padding-bottom:24rpx;
							.conimg{
								width:100rpx;
								height:100rpx;
								border-radius: 10rpx;
								margin-right:20rpx;
							}
							.config_name{
								width:290rpx;
								font-size:24rpx;
								color:#333;
								overflow: hidden;
								text-overflow:ellipsis;
								white-space: nowrap;
							}
							.config_step{
								
								margin-left:24rpx;
							}
						}
					}
				}
			}
			.right_del_btn{
				width:90rpx;
				height:240rpx;
				background:#F00;
				color:#fff;
				text-align: center;
				line-height: 224rpx;
			}
		}
	}
</style>